package com.love.marriage.cloud.service.impl;

import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import com.love.marriage.config.AliyunDyplsConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.love.marriage.cloud.config.MyMessageListener;

import javax.annotation.PostConstruct;

@Service
@RequiredArgsConstructor
public class MessagePullService {

    private final MyMessageListener myMessageListener;

    private final AliyunDyplsConfig aliyunDyplsConfig;

    @PostConstruct
    public void startPullingMessages() {
        try {
            DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();
            puller.setConsumeMinThreadSize(6);
            puller.setConsumeMaxThreadSize(16);
            puller.setThreadQueueSize(200);
            puller.setPullMsgThreadSize(1);
            puller.openDebugLog(false);
            // 启动消息监听
            puller.startReceiveMsg(aliyunDyplsConfig.getAccessKeyId(), aliyunDyplsConfig.getAccessKeySecret(), aliyunDyplsConfig.getMessageType(), aliyunDyplsConfig.getQueueName(), myMessageListener);
            System.out.println("Message listener started successfully.");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Failed to start message listener: " + e.getMessage());
        }
    }
}
